/**
 * Author: Lily Jiang
 * Create Time: 2019/12/24
 * Description:
 */

import React from "react";
import { connect } from "react-redux";
import * as actions from "@/store/actions";
import './Index.scss'
import AppHeader from '@/components/appHeader/Index.js'
import { Toast } from 'antd-mobile';

class ArticleCommentCreate extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            content: ''
        };
    }

    changeLoadingState = (status) => {
        this.props.dispatch(actions.updateAppSetting({
            appLoading: status
        }))
    }

    handleChange = (e) => {
        this.setState({
            content: e.target.value.trim()
        })
    }

    submit = () => {
        this.changeLoadingState(true)
        React.$api.commentAdd({
            article_id: this.props.location.searchObj.articleId,
            content: this.state.content,
            comment_id: this.props.location.searchObj.commentId || ''
        }).then(data => {
            this.changeLoadingState(false)
            window.history.back()
        }, err => {
            this.changeLoadingState(false)
            err.msg && Toast.info(err.msg)
        })
    }


    render() {
        return (
            <div className="app-container page_article-comment-create">
                {<AppHeader title={this.props.location.searchObj.title ? '回复' + this.props.location.searchObj.title : '写点评'} actionText="发布" action={this.submit}></AppHeader>}
                <div className="form">
                    <textarea placeholder="友善的评论是交流的起点" value={this.state.content} onChange={this.handleChange}></textarea>
                </div>
            </div>
        );
    }
}

export default connect()(ArticleCommentCreate)